/**
 对PPT的操作:保存 更新  发布
*/

var returnId = -1;
var introduction;//介绍
//存储着PPT里对应的内容
var pptContentA;

//定时器更新后 会将这里的值进行更新
var exTimerH=0;//测试
var exTimerM=0;//测试
var exTimerS=0;//测试

//制作PPT的页面跳转
var active=false;
var repeatFrom=1;
var repeatTo=1;
var playMode="order";
var repeatTime=1;

//自动保存
var autoSaveTicket;

/**
 * 保存
 */
$(".btn-save").click(function(){
	getPPTs();
	
	var title;//标题
	if($("#title").val().trim()==""){
		alert("标题不能为空");
		return;
	}
	
	if(introduction.trim()==""){
		alert("亲，请写一点关于这个项目的介绍");
		return;
	}
	title=$("#title").val().trim();
	
	var cellNO=ppt.NO;
	var examType="PRIVATE";//测试
	
	var check = $("#jump-on-web").attr("checked");
	if(check=="checked"||check==true){
		active = true;
	}
	else{
		active=false;
	}
	
	repeatFrom = $("#ppt-repeat-from").val();
	if(repeatFrom=="none"){
		repeatFrom=0;
	}

	repeatTo = $("#ppt-repeat-to").val();
	if(repeatTo=="none"){
		repeatTo=0;
	}
	
	playMode = $("#ppt-repeat-method").val();
	repeatTime=$("#ppt-repeat-times").val();
	if(repeatTime=="" || isNaN(repeatTime)){
		repeatTime=1;
	}
	
	var postdata = {
			id:returnId,
			title:title,
			introduction:introduction,
			cellNo:cellNO,
			active:active,
			repeatFrom:repeatFrom,
			repeatTo:repeatTo,
			playMode:playMode,
			repeatTime:repeatTime,
			exTimerH:exTimerH,
			exTimerM:exTimerM,
			exTimerS:exTimerS,
			examType:examType,
			'htmlContents[]':pptContentA
	};
	
	var url="../exam/save.html";
	//保存变更新
	if(returnId!=-1){
		url="../exam/update.html";
	}
	
	//return;
	$.ajax({
		url : url,
		data : postdata,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if (result > 0) {
				returnId=result;
				//保存成功 计时器清零,重新计算
				clearInterval(autoSaveTicket);
				autoSaveTicket = setInterval(autoSave, 3*60*1000);
				alert("保存成功");
			} else {
				alert("保存失败");
			}
		},
		error : function() {
			alert("连接服务器出错");
		}
	});
	
});


/**
 * 得到PPT里对应的内容
 */
function getPPTs(){
	//由于保存的机制是切换的时候，所以当点击保存时当前页可能没有保存
	ppt.contentA[ppt.curNO]=$(".c-content").html();
	
	var lis = $(".mo-content li");
	var j=0;
	pptContentA = new Array();
	
	for(var i = 0;i<lis.length;i++){
		var idStr = lis[i].id;
		if(idStr!=undefined){
			var id = parseInt(idStr.split("-")[1]);
			pptContentA[j]=ppt.contentA[id];
			j++;
		}
	}
}

/**
 * 发布:前提先保存
 */
$(".btn-publish").click(function(){

	getPPTs();
	
	var title;//标题
	if($("#title").val().trim()==""){
		alert("亲，你忘记写标题了");
		return;
	}
	
	if(introduction.trim()==""){
		alert("亲，请写一点关于这个项目的介绍");
		return;
	}
	title=$("#title").val().trim();
	var cellNO=ppt.NO;
	var examType="PRIVATE";//测试
	
	var check = $("#jump-on-web").attr("checked");
	if(check=="checked"||check==true){
		active = true;
	}
	else{
		active=false;
	}
	
	repeatFrom = $("#ppt-repeat-from").val();
	if(repeatFrom=="none"){
		repeatFrom=0;
	}

	repeatTo = $("#ppt-repeat-to").val();
	if(repeatTo=="none"){
		repeatTo=0;
	}
	
	playMode = $("#ppt-repeat-method").val();
	repeatTime=$("#ppt-repeat-times").val();
	if(repeatTime=="" || isNaN(repeatTime)){
		repeatTime=1;
	}
	
	var postdata = {
			id:returnId,
			title:title,
			introduction:introduction,
			cellNo:cellNO,
			active:active,
			repeatFrom:repeatFrom,
			repeatTo:repeatTo,
			playMode:playMode,
			repeatTime:repeatTime,
			exTimerH:exTimerH,
			exTimerM:exTimerM,
			exTimerS:exTimerS,
			examType:examType,
			'htmlContents[]':pptContentA
	};
	
	var url="../exam/save.html";
	//保存变更新
	if(returnId!=-1){
		url="../exam/update.html";
	}
	
	//return;
	$.ajax({
		url : url,
		data : postdata,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if (result > 0) {
				returnId=result;
				
				$.ajax({
					url : "../exam/publish.html",
					data : "id="+returnId+"&title="+title,
					type : 'post',
					dataType : 'json',
					contentType : "application/x-www-form-urlencoded;charset=utf-8",
					success : function(result) {
						if (result > 0) {
							alert("发布成功");
							window.location.href="../user/home.html";
						} else {
							alert("发布失败");
						}
					},
					error : function() {
						alert("连接服务器出错");
					}
				});
				
				
			} else {
				alert("发布失败");
			}
		},
		error : function() {
			alert("连接服务器出错");
		}
	});
	

});

/**
 * 制作PPT时 自动保存
 */
function autoSave(){
	getPPTs();
	//自动保存的前提是有内容写入 第一张为介绍
	var title = $("#title").val();
	if(pptContentA.length>1 || introduction!="" || title!=""){
		
		if(title==undefined || title.trim()==""){
			title = ""+getTime();
		}
		
		if(introduction==""){
			introduction="您还没有填写相关介绍";
		}
		
		var cellNO=ppt.NO;
		var examType="PRIVATE";
		
		var check = $("#jump-on-web").attr("checked");
		if(check=="checked"||check==true){
			active = true;
		}
		else{
			active=false;
		}
		
		repeatFrom = $("#ppt-repeat-from").val();
		if(repeatFrom=="none"){
			repeatFrom=0;
		}

		repeatTo = $("#ppt-repeat-to").val();
		if(repeatTo=="none"){
			repeatTo=0;
		}
		
		playMode = $("#ppt-repeat-method").val();
		repeatTime=$("#ppt-repeat-times").val();
		if(repeatTime=="" || isNaN(repeatTime)){
			repeatTime=1;
		}
		
		var postdata = {
				id:returnId,
				title:title,
				introduction:introduction,
				cellNo:cellNO,
				active:active,
				repeatFrom:repeatFrom,
				repeatTo:repeatTo,
				playMode:playMode,
				repeatTime:repeatTime,
				exTimerH:exTimerH,
				exTimerM:exTimerM,
				exTimerS:exTimerS,
				examType:examType,
				'htmlContents[]':pptContentA
		};
		
		var url="../exam/save.html";
		//保存变更新
		if(returnId!=-1){
			url="../exam/update.html";
		}
		
		$.ajax({
			url : url,
			data : postdata,
			type : 'post',
			dataType : 'json',
			contentType : "application/x-www-form-urlencoded;charset=utf-8",
			success : function(result) {
				if (result > 0) {
					returnId=result;
					$("#auto-save-info").html("自动保存于： "+getTime()+"  状态：未发布");
				} else {

				}
			},
			error : function() {
				//$("#auto-save-info").html("自动保存失败");
			}
		});
	}
}

/**
 * 当用户编程"介绍"时,更新到这边的introduction
 * @param val
 */
function updateIntroduction(val){
	introduction = val;
}


/**
 * 自动保存 每隔5分钟
 */
$(document).ready(function(){
	autoSaveTicket = setInterval(autoSave, 5*60*1000);
});

/**
 * 监听刷新和退出
 
window.onbeforeunload = onbeforeunload_handler;  
window.onunload = onunload_handler; 

function onbeforeunload_handler(){  
	
	return "";
}  
 //刷新 或者 关闭，删除
function onunload_handler(){  
    if(returnId!=-1){

		$.ajax({
			url : "../ppt/drop.html?id="+returnId,
			type : 'post',
			dataType : 'json',
			contentType : "application/x-www-form-urlencoded;charset=utf-8",
			success : function(result) {
			},
			error : function() {
				alert("连接服务器出错");
			}
		});
	
    }  
}  
*/